package com.chexiehui.jc.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chexiehui.jc.R;

import java.util.List;

public class ChooseGridAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mList;
    private int choosePosition;

    public ChooseGridAdapter(Context mContext, List<String> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_choose_grid, null);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_item_title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (!"".equals(mList.get(position))) {
            viewHolder.textView.setText(mList.get(position));
        }
        if (choosePosition == position) {
            viewHolder.textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.app_color));
            viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.colorWhite));
        } else {
            viewHolder.textView.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorBackground));
            viewHolder.textView.setTextColor(ContextCompat.getColor(mContext, R.color.c_333333));
        }
        return convertView;
    }

    /**
     * 修改选中时的状态
     *
     * @param position
     */
    public void changeState(int position) {
        if (position == choosePosition) {
            return;
        }
        choosePosition = position;
        notifyDataSetChanged(); // 通知适配器进行更新
    }

    public int getChoosePosition() {
        return choosePosition;
    }

    private class ViewHolder {
        TextView textView;
    }
}
